package com.itheima.demo3udp2;

// 1、定义一个线程任务类实现Runnable接口
public class MyRunnable implements Runnable {
    private boolean mode;
    private int offset;
    private int port;
    public MyRunnable(boolean mode,int offset,int port)
    {
        this.mode = mode;
        this.offset = offset;
        this.port = port;
    }
    // 2、重写run方法，设置线程任务
    @Override
    public void run() {
        ChatClass chatClass = new ChatClass(offset);
        if (mode) {
            while (true) {
                try {
                    chatClass.send(port);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        else{
            while (true) {
                try {
                    chatClass.receive(port);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
